package com.qiche.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * PropertiesUtil : 配置文件工具类
 *
 * @author zhangds
 * @since 2017-04-14 9:36
 */
public class PropertiesUtils {

    /**
     * 读取配置文件application.properties指定的属性值
     * @param key 指定的key
     * @return 取得的值string类型
     */
    public static String getPropertiesValueAsString(String key) {
        //读取配置文件
        InputStream in = PropertiesUtils.class.getResourceAsStream("/application.properties");
        Properties pro = new Properties();
        String result = "";

        try {
            pro.load(in);
            result = pro.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 读取指定的配置文件的指定值
     * @param propath 配置文件路径
     * @param key 指定key
     * @return 取得的值string类型
     */
    public static String getPropertiesValueAsString(String propath,String key) {
        //读取配置文件
        InputStream in = PropertiesUtils.class.getResourceAsStream(propath);
        Properties pro = new Properties();
        String result = "";

        try {
            pro.load(in);
            result = pro.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    /**
     * 读取配置文件application.properties指定的属性值
     * @param key 指定的key
     * @return 取得的值int类型
     */
    public static int getPropertiesValueAsInt(String key) {
        //读取配置文件
        InputStream in = PropertiesUtils.class.getResourceAsStream("/application.properties");
        Properties pro = new Properties();
        String result = "";

        try {
            pro.load(in);
            result = pro.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Integer.parseInt(result);
    }


    /**
     * 读取指定的配置文件的指定值
     * @param propath 配置文件路径
     * @param key 指定key
     * @return 取得的值int类型
     */
    public static int getPropertiesValueAsInt(String propath,String key) {
        //读取配置文件
        InputStream in = PropertiesUtils.class.getResourceAsStream(propath);
        Properties pro = new Properties();
        String result = "";

        try {
            pro.load(in);
            result = pro.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Integer.parseInt(result);
    }


    /**
     * 读取配置文件application.properties指定的属性值
     * @param key 指定的key
     * @return 取得的值Long类型
     */
    public static Long getPropertiesValueAsLong(String key) {
        //读取配置文件
        InputStream in = PropertiesUtils.class.getResourceAsStream("/application.properties");
        Properties pro = new Properties();
        String result = "";

        try {
            pro.load(in);
            result = pro.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Long.parseLong(result);
    }


    /**
     * 读取指定的配置文件的指定值
     * @param propath 配置文件路径
     * @param key 指定key
     * @return 取得的值Long类型
     */
    public static Long getPropertiesValueAsLong(String propath,String key) {
        //读取配置文件
        InputStream in = PropertiesUtils.class.getResourceAsStream(propath);
        Properties pro = new Properties();
        String result = "";

        try {
            pro.load(in);
            result = pro.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Long.parseLong(result);
    }
}
